//
//  MeatMarketListVIewController.m
//  PigMarket
//
//  Created by 张敬 on 2019/7/15.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "MeatMarketListViewController.h"
#import "MeatMarketsViewController.h"

#import "MeatMarketsHeader.h"


@interface MeatMarketListViewController ()

@property (nonatomic, strong) NSArray *titles;
@property (nonatomic, strong) NSArray *controls;
@property (nonatomic, strong) MeatMarketsHeader *header;


@end

@implementation MeatMarketListViewController

#pragma mark - view life cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    
}

- (void)viewWillLayoutSubviews{
    
    [super viewWillLayoutSubviews];
    self.contentScrollView.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight);
}


#pragma mark - init method

- (void)setUpView {
    [self setupNavBar];
    [self setupChildViewControllers];
    [self setupChildSetting];
}

- (void)setupNavBar{
    [self.view addSubview:self.header];
}

- (void)setupChildViewControllers{

    kWeakSelf(self);
    [self.controls enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

        UIViewController *vc = (UIViewController *)obj;
        vc.title = weakself.titles[idx];
//        vc.block = ^(NSInteger index) {
//           weakself.selectIndex = index;
//        };
        [weakself addChildViewController:vc];
    }];
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    kWeakSelf(self);
    [self setUpContentViewFrame:^(UIView *contentView) {
        CGFloat contentX = 0;
        CGFloat contentH = KScreenHeight - weakself.header.height - TJ_TabBarHeight;
        contentView.frame = CGRectMake(contentX, weakself.header.height+10, KScreenWidth, contentH);
    }];
    
}

- (void)setupChildSetting
{
    kWeakSelf(self);
    [self setUpContentViewFrame:^(UIView *contentView) {
        CGFloat contentX = 0;
        CGFloat contentH = KScreenHeight - weakself.header.height - TJ_TabBarHeight ;
        contentView.frame = CGRectMake(contentX, weakself.header.height, KScreenWidth, contentH);
    }];
    
    [self setUpTitleEffect:^(UIColor *__autoreleasing *titleScrollViewColor, UIColor *__autoreleasing *norColor, UIColor *__autoreleasing *selColor, UIFont *__autoreleasing *titleFont, CGFloat *titleHeight,CGFloat *titleWidth) {
        
        // 设置标题字体
        *titleFont = mFont([weakself fitSize:15]);
        *norColor = UIColorFromRGB(0x333333);
        *selColor = mMainColor;
        *titleHeight = [weakself fitSize:42];
        *titleScrollViewColor = KWhiteColor;
        CGSize size = [weakself.titles[0] getTextSizeWith:mFont([weakself fitSize:14]) maxWidth:MAXFLOAT maxHeight:30];
        // *titleWidth = size.width + [weakself fitSize:33];
    }];
    
    // 推荐方式（设置下标）
    [self setUpUnderLineEffect:^(BOOL *isUnderLineDelayScroll, CGFloat *underLineH, UIColor *__autoreleasing *underLineColor,BOOL *isUnderLineEqualTitleWidth) {
        // 标题填充模式
        *underLineColor = mMainColor;
        *isUnderLineEqualTitleWidth = YES;
    }];
}

////改变地址后 刷新
//- (void)reloadArea
//{
//    self.navBar.locationStr = [kUserDefaults objectForKey:kLocationTitleKey];
//    [_addressDic setObject:[kUserDefaults objectForKey:kLocationTitleKey] forKey:@"address"];
//    //    [self getHomeNavMeunData];
//    if (self.contentScrollView) {
//        CGFloat offsetX = self.contentScrollView.contentOffset.x;
//        NSInteger index = offsetX/KScreenWidth;
//        if (index < self.childViewControllers.count) {
//            XXTGChildViewController * vc = self.childViewControllers[index];
//            [vc needReloadData];
//        }
//    }
//}


#pragma mark - setter and getter
- (NSArray *)titles{
    if (!_titles) {
        _titles = @[@"卖肉",@"同城掌柜",@"我的肉铺"];
    }
    return _titles;
}

- (NSArray *)controls
{
    if (!_controls) {
        
        MeatMarketsViewController *oneVC = [[MeatMarketsViewController alloc] init];
        UIViewController *twoVC = [[UIViewController alloc]init];
        UIViewController *threeVC = [[UIViewController alloc]init];
        
        oneVC.view.backgroundColor = KRedColor;
        twoVC.view.backgroundColor = KYellowColor;
        threeVC.view.backgroundColor = KBlueColor;
        
        _controls = @[oneVC,twoVC,threeVC];
    }
    return _controls;
}


- (MeatMarketsHeader *)header{
    if (!_header) {
        _header = [[MeatMarketsHeader alloc] init];
    }
    return _header;
}


@end
